package com.maltys.sign_aspect.util;

import com.maltys.sign_aspect.ErrorCode;
import com.maltys.sign_aspect.enums.SignTypeEnum;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

/**
 * @author luonankun
 * @date 2022/10/21
 **/
@Slf4j
public class SignContext {
    private final SignStrategy signStrategy;

    public SignContext(SignTypeEnum signType) throws Exception {
        if (SignTypeEnum.SERVER == signType) {
            this.signStrategy = new ServerSignStrategy();
        } else if (SignTypeEnum.CLIENT == signType) {
            this.signStrategy = new ClientSignStrategy();
        } else {
            log.info("#SignContext.SignContext, sign type: {} , sign type not support! \n", signType);
            throw new Exception(ErrorCode.APP_SIGNATURE_ERROR.getMsg());
        }
    }

    public Boolean checkSign(HttpServletRequest request, String signKey, int[][] appKeyIdx) throws Exception {
        return signStrategy.checkSign(request, signKey, appKeyIdx);
    }
}
